﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication0
{
    /// <summary>
    /// Summary description for login
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class login : System.Web.Services.WebService
    {

        [WebMethod]
        public bool IsValidUser(string username, string password)
        {
            System.Data.SqlClient.SqlConnection connect = new System.Data.SqlClient.SqlConnection();
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
            System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();

            connect.ConnectionString = "data source = (local); initial catalog = Incubator; User ID =liang; Password =zhang;";
            command.Connection = connect;

            command.CommandText = "SELECT * FROM userinfo WHERE username ='" + username + "'";

            adapter.SelectCommand = command;
            
            System.Data.DataTable dt = new System.Data.DataTable();
            adapter.Fill(dt);

            //return (dt.Rows[0][1].Equals(password)) ? true : false;
            return dt.Rows[0][1].ToString() == password ? true : false;
        }

        [WebMethod]
        public string GetPassword(string username)
        {
            System.Data.SqlClient.SqlConnection connect = new System.Data.SqlClient.SqlConnection();
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
            System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();

            connect.ConnectionString = "data source = (local); initial catalog = Incubator; User ID =liang; Password =zhang;";
            command.Connection = connect;

            command.CommandText = "SELECT * FROM userinfo WHERE username ='" + username + "'";

            adapter.SelectCommand = command;

            System.Data.DataTable dt = new System.Data.DataTable();
            adapter.Fill(dt);

            return dt.Rows[0][1].ToString();
        }
    }
}
